<?php
declare (strict_types=1);

namespace app\course\service\exam\admin;

use app\common\service\BaseService;
use app\common\logic\category\admin\Category;
use app\course\logic\base\admin\Course;
use app\course\logic\exam\admin\CourseExamLink;
use app\exam\logic\admin\Paper as PaperLogic;
use eduline\admin\libs\pageform\FormItem;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageForm;
use eduline\admin\page\PageList;
use think\annotation\Inject;
use think\facade\Request;

/**
 * Class Paper
 *
 * @package app\course\service\exam\admin
 */
class Paper extends BaseService
{
    /**
     * @Inject()
     *
     * @var CourseExamLink
     */
    protected $courseExamLinkLogic;

    /**
     * @Inject()
     *
     * @var Category
     */
    protected $categoryLogic;

    /**
     * @Inject()
     *
     * @var PaperLogic
     */
    protected $paperLogic;

    /**
     * @Inject()
     *
     * @var Course
     */
    protected $courseLogic;

    /**
     * index
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/9/28
     *
     * @return \eduline\admin\page\interfaces\think\Response
     * @throws \think\db\exception\DbException
     */
    public function index(int $courseId)
    {
        $map  = [
            'course_id' => $courseId
        ];
        $data = $this->courseExamLinkLogic->getList($map);
        // 定义字段
        $keyList = [
            'paper_title' => ListItem::make()->title('试卷名称'),
            'pass_score'  => ListItem::make()->title('通过分数'),
            'update_time' => ListItem::make()->title('更新时间')
        ];

        // 设置表单
        $list = new PageList();
        // 表单字段
        $list->pageKey = $keyList;
        $list->datas   = $data;

        return $list->send();
    }

    /**
     * 关联试卷
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/9/28
     *
     * @return mixed
     */
    public function create(int $courseId)
    {
        if (Request::isPost()) {
            $data = Request::post();
            $res  = $this->courseExamLinkLogic->createCourseExamLink($courseId, $data);
            return $this->parseToData($res->toArray(), 1, '添加成功');
        }

        // 设置表单
        $form = app(PageForm::class);
        // 表单字段
        $form->pageKey = $this->keyListInit();

        $form->saveMethod  = 'POST';
        $form->savePostUrl = '/course/admin/exam/' . $courseId . '/create';

        return $form->send();
    }

    /**
     * 编辑
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/9/28
     *
     * @param int $courseId
     * @param int $paperId
     * @return \think\Response
     */
    public function edit(int $courseId, int $paperId)
    {
        if (Request::isPut()) {
            $data = Request::put();
            $this->courseExamLinkLogic->editCourseExamLink($courseId, $paperId, $data);
            return $this->parseToData([], 1, '编辑成功');
        }

        $data = $this->courseExamLinkLogic->with(['paper_title'])->where('course_id', $courseId)->where('paper_id', $paperId)->findOrFail();

        // 设置表单
        $form = app(PageForm::class);
        // 表单字段
        $form->pageKey = $this->keyListInit(true, $data);
        $form->datas   = $data;

        $form->saveMethod  = 'PUT';
        $form->savePostUrl = '/course/admin/exam/' . $courseId . '/' . $paperId . '/edit';

        return $form->send();
    }

    /**
     * 初始化字段
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-04
     */
    protected function keyListInit($update = false, $data = null)
    {
        if ($update) {
            $keyList = [
                'paper_title' => FormItem::make('word')->title('试卷')->required(),
                'pass_score'  => FormItem::make('inputNumber')->title('通过分数')->required()->min(0),
            ];
        } else {
            $keyList = [
                'paper_id'   => FormItem::make('custom')->title('试卷')->required(),
                'pass_score' => FormItem::make('inputNumber')->title('通过分数')->required()->min(0),
            ];
        }

        return $keyList;
    }

    /**
     * 删除
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/9/29
     *
     * @param int $courseId
     * @param int $paperId
     * @return \think\Response
     */
    public function delete(int $courseId, int $paperId)
    {
        $this->courseExamLinkLogic->deleteCourseExamLink($courseId, $paperId);

        return $this->parseToData([], 1, '删除成功');
    }

    /**
     * 批量删除
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/9/29
     *
     * @param $ids
     * @return \think\Response
     */
    public function deletes(int $courseId, $ids)
    {
        $this->courseExamLinkLogic->deleteCourseExamLinks($courseId, $ids);

        return $this->parseToData([], 1, '删除成功');
    }

    /**
     * 选择面板数据
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/10/8
     *
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getChooseOptions(int $courseId)
    {
        $courseMhmId = $this->courseLogic->where('id', $courseId)->value('mhm_id');
        $category    = $this->categoryLogic->cascader(0, 3, intval($courseMhmId));
        $paperType   = $this->paperLogic->getPaperTypeOptions();

        return BaseService::parseToData([
            'options' => [
                'category'   => $category,
                'paper_type' => $paperType,
            ]
        ], 1);
    }

    /**
     * 获取课程试卷;列表
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/10/8
     *
     * @param int $courseId
     * @return \eduline\admin\page\interfaces\think\Response
     * @throws \think\db\exception\DbException
     */
    public function getChooseList(int $courseId)
    {
        $map = [];
        // 关键字
        Request::has('title', 'get', true) && $map[] = ['title', 'like', '%' . Request::get('title/s') . '%'];
        // 试卷类型
        if (Request::has('paper_type', 'get', true) && Request::get('paper_type/d', -1) !== -1) {
            $map[] = ['paper_type', '=', Request::get('paper_type/d')];
        }
        // 分类
        $category = Request::get('category/s', 0);
        $category && $map[] = ['category', '=', $category];

        // 难易度
        $difficulty = Request::get('difficulty/d', 0);
        $difficulty && $map[] = ['point', '=', $difficulty];
        $data = $this->courseExamLinkLogic->getChooseList($courseId, $map);
        // 设置表单
        $list = new PageList();
        // 表单字段
        $list->datas = $data;

        return $list->send();
    }
}